/*一. 自定义校验*/
// 手机号码验证
export const validatePhone = (rule, value, callback) => {
  const patter = new RegExp("^1[3|4|5|6|7|8|9][0-9]{9}$");
  if (!value) {
    callback();
  } else if (!patter.test(value)) {
    return callback(new Error("请输入正确格式的手机号"));
  } else {
    callback();
  }
};
// 邮箱的正则
export const validateEmail = (rule, value, callback) => {
  const mailbox = new RegExp("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$");
  if (!value) {
    callback();
  } else if (!mailbox.test(value)) {
    return callback(new Error("请输入正确格式的邮箱"));
  } else {
    callback();
  }
};
// 2. 身份证号
export const validateIdCardNo = (rule, value, callback) => {
  const mailbox = new RegExp(
    "^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$"
  );
  if (!value) {
    callback();
  } else if (!mailbox.test(value)) {
    return callback(new Error("请输入正确格式的身份证号！"));
  } else {
    callback();
  }
};
// 3. 账号校验
export const valiAccountPass = (rule, value, callback) => {
  let reg = /^[a-zA-Z0-9./_@]+$/;
  if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("请输入数字、字母或./_@"));
  }
};
//4. 密码校验
export const valiPasswordPass = (rule, value, callback) => {
  let reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,12}$/;
  if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("用户名密码必须包含数字大小写字母和特殊字符"));
  }
};
//5. 银行卡号
// 银行卡号校验函数
export const checkCardNum = (rule, value, callback) => {
  const reg = /^([1-9]{1})(\d{14}|\d{18})$/;
  if (!value) {
    callback();
  } else if (reg.test(value)) {
    callback();
  } else {
    callback(new Error("银行卡号格式不正确"));
  }
};

// 6. 校验单个ip
export function singleIpCheck(rule, value, callback) {
  console.info(value);
  if (value == "" || value == undefined) {
    callback();
  }
  if (value?.indexOf("/") > -1) {
    callback(new Error("IP地址格式错误"));
  }
  if (value?.indexOf("-") > -1) {
    callback(new Error("IP地址格式错误"));
  }
  const iplist =
    /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))|\*)((\/([012]\d|3[012]|\d))?)(,((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))|\*)((\/([012]\d|3[012]|\d))?))*$/;
  const ipduan =
    /^(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))-(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\D))$/;
  const ipv6 =
    /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
  const iparr = value; // .split(",");
  const ip4 = !iplist.test(iparr);
  const ipduans = !ipduan.test(iparr);
  const ip6 = !ipv6.test(iparr);
  if (ip4 && ipduans && ip6) {
    callback(new Error("IP地址格式错误"));
  }
  callback();
}

export function singleIpCheckFun(value) {
  console.info(value);
  if (value == "" || value == undefined) {
    return true;
  }
  console.log(value);
  if (value.indexOf("/") > -1) {
    return false;
  }
  const iplist =
    /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))|\*)((\/([012]\d|3[012]|\d))?)(,((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))|\*)((\/([012]\d|3[012]|\d))?))*$/;
  const ipduan =
    /^(?=(\b|\D))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\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9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
  const iparr = value; // .split(",");
  const ip4 = !iplist.test(iparr);
  const ipduans = !ipduan.test(iparr);
  const ip6 = !ipv6.test(iparr);
  if (ip4 && ipduans && ip6) {
    return false;
  }
  return true;
}

/**
 * @desc 7. 验证端口
 * @param {*} msg
 * @param {*} trigger
 * @returns
 */
export function validatePortCheck(rule, value, callback) {
  const vPort = (str, min = 2, max = 5) => {
    const reg = new RegExp(
      `^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0][0-9]{1,4})|([0-9]{1,4}))$`
    );
    return reg.test(str);
  };
  if (value == "" || value == undefined) {
    callback();
  }
  if (value == "0") {
    callback();
  } else if (value && vPort(value)) {
    callback();
  } else {
    callback(new Error(rule.message || "端口格式错误"));
  }
}

export function validatePortFun(value) {
  const vPort = (str, min = 2, max = 5) => {
    const reg = new RegExp(
      `^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0][0-9]{1,4})|([0-9]{1,4}))$`
    );
    return reg.test(str);
  };
  if (value == "" || value == undefined) {
    return true;
  }
  if (value == "0") {
    return true;
  } else if (value && vPort(value)) {
    return true;
  } else {
    return false;
  }
}

/*二. 常用验证*/

//验证非空
export const requiredCheck = (msg, trigger = "blur") => [
  {
    required: true,
    message: msg,
    trigger: trigger
  }
];

//校验字数范围
export const rangeCheck = ({ min, max, message }) => [
  {
    min: min,
    max: max,
    message: message || `长度${min}到${max}个字符`,
    trigger: ["blur", "change"]
  }
];
//手机号码验证正则
export const telCheck = [
  {
    pattern: /^1[3456789]\d{9}$/,
    message: "手机号格式不对",
    trigger: ["blur", "change"]
  }
];

//身份证验证
export const idCardCodeCheck = [
  {
    pattern:
      /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
    message: "证件号码格式有误！",
    trigger: "blur"
  }
];

//校验是否为数字
export const numberCheck = [
  {
    pattern: /^[0-9]*$/,
    message: "请输入数字",
    trigger: ["blur", "change"]
  }
];

//校验座机号
export const landlineNumberCheck = [
  {
    pattern: /^\d{3,4}\-\d{7,8}$/,
    message: "座机不正确,格式为：区号-座机号，例如：	07558886666",
    trigger: "blur"
  }
];

//校验是否为汉字
export const hanzi = [
  {
    pattern: /^[\u4e00-\u9fa5]{0,}$/,
    message: "姓名格式错误,请检查",
    trigger: "blur"
  }
];

//金额校验,允许负数,两位小数
export const monnyPositiveCheck = [
  {
    pattern: /(^([-]?)[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^([-]?)(0){1}$)|(^([-]?)[0-9]\.[0-9]([0-9])?$)/,
    message: "请输入正确的金额！",
    trigger: "blur"
  }
];

//金额校验,不允许负数,两位小数
export const monnyAllCheck = [
  {
    pattern: /^\d+\.?\d{0,2}$/,
    message: "请输入正确的金额！",
    trigger: "blur"
  }
];
export const titleCheck = [
  {
    pattern: /^[\u4e00-\u9fa50-9-]+$/,
    message: "标题仅限汉字数字和短横线（-）！",
    trigger: ["blur"]
  }
];

function vDomian(ipdata) {
  const iparray = ipdata.split(/\r\n|\r|\n/);
  for (let item = 0; item < iparray.length; item++) {
    const itemdata = iparray[item];

    var strRegex = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
    var re = new RegExp(strRegex);
    if (re.test(itemdata) == false && itemdata.length > 0) {
      return false;
    }
  }
  return true;
}

// 检测域名
export function validDomian(rule, value, callback) {
  if (value == "") {
    callback();
  } else if (value && vDomian(value)) {
    callback();
  } else {
    callback(new Error("域名格式错误"));
  }
}

export function vMac(str) {
  const reg = /^((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$/i;
  return reg.test(str);
}

export function validMultiMac(rule, value, callback) {
  if (value == "") {
    callback(new Error("MAC地址格式错误"));
  } else if (value) {
    // console.info(value);
    const iparray = value.split(/\r\n|\r|\n/);
    // console.info(iparray);
    for (let item = 0; item < iparray.length; item++) {
      // console.info(iparray[item])
      if (/\s/.test(iparray[item])) {
        callback(new Error(rule.message || "MAC地址中不能包含空格"));
        return false;
      }
      const validMac = iparray[item].trim();
      if (vMac(validMac) == false) {
        return callback(new Error(rule.message || "MAC地址格式错误"));
      }
    }
    callback();
  } else {
    callback(new Error(rule.message || "MAC地址 格式错误"));
  }
}
